package com.appspot.zarm.gomoku;

import java.util.ArrayList;
import java.util.List;

import com.appspot.zarm.game.Board;
import com.appspot.zarm.gomoku.transfer.BoardExternalizer;
import com.appspot.zarm.gomoku.transfer.BoardState;

public class GomokuBoard implements Board, BoardExternalizer{

	private List<Cell> cells;
	private int size;

	public GomokuBoard(int size) {
		this.size = size;
		cells = new ArrayList<Cell>();
		int length = size * size;
		for (int i = 0; i < length; i++) {
			cells.add(new Cell());
		}
	}
	
	public Cell getCell(Coord position) {
		return cells.get(getRelativePosition(position));
	}

	public void setCell(Coord position, Cell value) {
		cells.set(getRelativePosition(position), value);
	}
	
	private int getRelativePosition(Coord position) {
		return position.getX() + position.getY() * size;
	}

	@Override
	public BoardState getBoardState() {
		List<CellState> cellStates = new ArrayList<CellState>(size);
		for (Cell cell:cells){
			cellStates.add(cell.getCellState());
		}

		return new BoardState(cellStates, size);
	}

}
